package com.nulp.psycho.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nulp.psycho.db.model.User;
import com.nulp.psycho.db.service.UserService;



@WebServlet("/ConfirmAcountSrevlet")
public class ConfirmAccountServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public ConfirmAccountServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String hash = request.getParameter("hash");

		UserService service = new UserService();
		boolean confirm = false;
		User user = service.getUserByHash(hash);

		if (user != null) {
			user.setConfirmationStatus(true);
			service.update(user);
			System.out.println(user);
			confirm = true;
			request.getSession().setAttribute("user", user);
			request.getSession().setAttribute("confirm", confirm);
			
			response.sendRedirect("home");
		} else {
			response.sendRedirect("home");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
